package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.lang.Assert;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.enums.EmbedOperateEnum;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.EmbedOperateService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lilh
 * @date 2021/4/7 13:30
 */
@Service
public class EmbedOperateManager implements InitializingBean {

    @Autowired
    private List<EmbedOperateService> operateServices;

    private Map<EmbedOperateEnum, EmbedOperateService> operateServiceMap;

    public void doOperate(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String operate = request.getParameter("operate");
        Assert.notBlank(operate, "参数缺失");
        EmbedOperateService operateService = this.operateServiceMap.get(EmbedOperateEnum.getOperate(operate));
        Assert.notNull(operateService);
        operateService.doOperate(request, response);
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        this.operateServiceMap = operateServices.stream().collect(Collectors.toMap(EmbedOperateService::getOperate, item -> item));
    }
}
